package cn.swu.lyx.homework2

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.View
import android.widget.EditText
import android.widget.ProgressBar
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import org.w3c.dom.Text
import java.util.*

class MainActivity : AppCompatActivity() {

    val TAG="!!MMainActivity"
    private var username:String?=null
    private var password:String?=null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val inputName=findViewById<EditText>(R.id.username)
        val inputpassword=findViewById<EditText>(R.id.password)
        
        //监听用户名情况，用日志打出 继承TextWatcher 要重写三个方法
        val userWatcher= object : TextWatcher {
            //文本改变前调用
            override fun beforeTextChanged(s: CharSequence?, start: Int, after: Int, count: Int) {
                Log.i(TAG,"beforeTextChanged:$s")
                //采用了日志打印 i为第三等级
                //CharSequence s：文本改变之前的内容
                //int start：文本开始改变时的起点位置，从0开始计算
                //int after：改变后添加的文本字数，即替代选中文本后的文本字数
                //int count：要被改变的文本字数，即将要被替代的选中文本字数
                //在当前文本s中，从start位置开始之后的count个字符（即将）要被after个字符替换掉
            }

            override fun onTextChanged(s: CharSequence?, start: Int, after: Int, count: Int) {
                Log.i(TAG,"onTextChanged:$s")
            }

            override fun afterTextChanged(s: Editable?) {
                Log.i(TAG,"afterTextChanged:$s")
                username=s?.toString() //将该改后的文本存入变量username
            }
        }
        ////监听密码情况，用日志打出 继承TextWatcher 要重写三个方法
        val passwordWatcher=object :TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, after: Int, count: Int) {
                Log.i(TAG,"beforeTextChanged:$s")
            }

            override fun onTextChanged(s: CharSequence?, start: Int, after: Int, count: Int) {
                Log.i(TAG,"onTextChanged:$s")
            }

            override fun afterTextChanged(s: Editable?) {
                Log.i(TAG,"afterTextChanged:$s")
                password=s?.toString()
            }
        }
        inputName.addTextChangedListener(userWatcher)
        inputpassword.addTextChangedListener(passwordWatcher)
    }
    //设置登录事件
    fun onClick(view:View){
        //日志事件 输出最终确定的用户名和密码
        Log.d(TAG,"username=$username")
        Log.d(TAG,"password=$password")
        val context=this

        //消息弹框设置，用于消息的确认
        AlertDialog.Builder(this).apply {
            setTitle("请确认")
            setMessage("是否进入银欣泰管理页面")
            setNegativeButton("取消"){
                dialog,which->Toast.makeText(context,"已取消",Toast.LENGTH_SHORT)
            }
            setPositiveButton("确定"){
                dialog,which->loginPage()
            }
        }.show()
    }

    private fun loginPage() {
        val process=findViewById<ProgressBar>(R.id.progressBar)
        val context=this
        //当点击后进度条开始显示 从零开始
        process.visibility=View.VISIBLE
        process.progress=0
        //模拟请求网络耗时
        val timer= Timer()
        val task=object :TimerTask(){
            override fun run() {
                //日志打印查看进度条情况
                Log.d(TAG,"progress=${process.progress}")
                process.progress=process.progress+10
                if(process.progress==20){
                    timer.cancel()
                    Handler(Looper.getMainLooper()).post {
                        process.visibility = View.GONE
                        val intent=Intent(context,managementSystem::class.java)
                        startActivity(intent)
                    }
                }
            }
        }
        timer.schedule(task,0,1000)
    }
}